package com.itbuka.search.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.itbuka.entity.Result;
import com.itbuka.goods.domain.Detail;
import com.itbuka.goods.feign.GoodsFeign;
import com.itbuka.search.domain.DetailIndex;
import com.itbuka.search.mapper.ESManagerMapper;
import com.itbuka.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    private ESManagerMapper esManagerMapper;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private GoodsFeign goodsFeign;

    @Override
    public void createMappingAndIndex() {
        //创建索引库
        elasticsearchTemplate.createIndex(DetailIndex.class);
        //创建映射
        elasticsearchTemplate.putMapping(DetailIndex.class);
    }

    @Override
    public void importById(String id) {
        Map map = new HashMap<>();
        map.put("grant_type", "client_credentials");
        map.put("client_id", "itbuka");
        map.put("client_secret", "123456");
        String post = HttpUtil.post("http://localhost:9200/oauth/token", map);
        Map token = JSONObject.parseObject(post, Map.class);
        String accessToken = token.get("access_token").toString();
        //参数
        Map param = new HashMap<>();
        param.put("id", id);
        //请求头
        Map header = new HashMap<>();
        header.put("Authorization","Bearer "+accessToken);

        //获取商品数据
        String body = HttpUtil.createGet("http://localhost:9011/detail/findDetail").addHeaders(header).form(param).execute().body();

        //获取商品数据
        Detail data = JSONObject.parseObject(JSONObject.parseObject(body, Result.class).getData().toString(), Detail.class);
        //商品转成索引类型
        DetailIndex detailIndex = JSONObject.parseObject(JSONObject.toJSONString(data), DetailIndex.class);
        //保存索引库
        esManagerMapper.save(detailIndex);
    }

    @Override
    public void deleteById(String id) {
        esManagerMapper.deleteById(id);
    }
}